var Global = require("../util/global.js");
var websocket = require("../util/websocket.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {
    },

    onLoad () {
    },

    start () {
        this.init();
    },
    init(){
        this.initWebSocket();
    },
    initWebSocket(){
        if(!Global.socket||!Global.socket.isopen){
            cc.log("连接websocket");
            var websocketUrl = Global.wsUrl;
            Global.socket = websocket.connect(websocketUrl,this);
            Global.socket.sock.onmessage = this.onMessage;
            Global.socket.sock.onopen = this.onOpen;
        }
    },
    requestGame(){
        message.type = 100010;
        message.content = {};
        message.content.userId = Global.userId;
        Global.socket.sendData(message);
    },
    requestMatch(){
        message.type = 100020;
        message.content = {};
        Global.socket.sendData(message);
    },
    requestAttack(){
        this.shoe.node.angle = -90+this.angleValue;

        message.type = 100080;
        message.content = {};
        Global.socket.sendData(message);
    },
    onOpen(){
        var self = this.parent;

        self.requestGame();
    },
    onMessage(event){
        cc.log("接收消息:" + event.data);

        var self = this.parent;

        var message = JSON.parse(event.data);

        if(message.type==200010){
        }else if(message.type==200020){
            
        }else if(message.type==200050){
            Global.room = message.content;
            cc.director.loadScene('room');
        }
    },
    onReqMatchClick(){
        this.requestMatch();
    }

    // update (dt) {},
});
